public class MaxSubarraySum {
    public static int maxSubArraySum(int[] nums) {
        if (nums == null || nums.length == 0) {
            throw new IllegalArgumentException("null");
        }
        
        int maxCurrent = nums[0];
        int maxGlobal = nums[0];
        
        for (int i = 1; i < nums.length; i++) {
            maxCurrent = Math.max(nums[i], maxCurrent + nums[i]);
            
            if (maxCurrent > maxGlobal) {
                maxGlobal = maxCurrent;
            }
        }
        
        return maxGlobal;
    }
    
    public static void main(String[] args) {
        int[] arr1 = {1, -2, 3, 5, -1};
        int[] arr2 = {1, -2, 3, -8, 5, 1};
        int[] arr3 = {1, -2, 3, -2, 5, 1};
        
        System.out.println("[1, -2, 3, 5, -1]: " + maxSubArraySum(arr1));
        System.out.println("[1, -2, 3, -8, 5, 1]: " + maxSubArraySum(arr2));
        System.out.println("[1, -2, 3, -2, 5, 1]: " + maxSubArraySum(arr3));
    }
}
    